home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15430 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.1 KB  |  31 lines

  1. Newsgroups: comp.lang.c
  2. Path: thinkage.on.ca!atbowler
  3. From: atbowler@thinkage.on.ca (Alan Bowler)
  4. Subject: Re: saving system()'s output to a C variable?
  5. Message-ID: <Dq2uBo.5vE@thinkage.on.ca>
  6. Sender: news@thinkage.on.ca
  7. Organization: Thinkage Ltd.
  8. References: <96104.233554U61441@uicvm.uic.edu> <smryanDpuDMI.Cyq@netcom.com>
  9. Date: Thu, 18 Apr 1996 21:28:35 GMT
  10.  
  11. In article <smryanDpuDMI.Cyq@netcom.com> smryan@netcom.com (@#$%!?!) writes:
  12. >:   Is it possible to save the output of the system() command to
  13. >: a C variable? For example, when I have the line system("date")
  14. >: in my C program, I want the output of this command to go to a
  15. >: C variable (e.g. char array) instead of going on the stdout.
  16. >: Please send me email at swaheed@uic.edu
  17. >
  18. >If your system has pipes or the like, you can use popen/pclose.
  19. >Otherwise, just the save the output to a file and then read the
  20. >file.
  21. >
  22. >    system("date >xx");
  23. >    f = fopen("xx","r");
  24. >    fgets(array,size,f);
  25. >    fclose(f);
  26.  
  27. This is likely to be the most widely portable technique.
  28. Rather than clobbing the user's file "xx", it would be
  29. a better idea to use tmpnam(), and arrange to delete
  30. the file when you are done.  
  31.